package com.mhg.socket;

import java.net.*;
import java.io.*;
/*
服务器端有反馈的版本
	客户端的 out 就是 服务端的 in
	客户端的 in  就是 服务端的 out
*/
//客户端
class Demo_02_Tcp_Client 
{
	public static void main(String[] args) throws Exception
	{
		//1.创建客户端Scoket
		Socket socket = new Socket("127.0.0.1",10011);
		//2.获取输出流
		OutputStream out = socket.getOutputStream();
		out.write("tcp 我又来啦".getBytes());
		
		//3.客户端接收 服务端反馈
		InputStream in = socket.getInputStream();
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		System.out.println(new String(buf,0,len));
	}
}
//服务端
class Demo_02_Tcp_Server
{
	public static void main(String[] args) throws Exception{
		//1.创建服务端ServerSocket
		ServerSocket ss = new ServerSocket(10011);
		//2.获取客户端连接
		Socket socket = ss.accept();
		InputStream in = socket.getInputStream();
		byte[] arr = new byte[1024];
		int len = in.read(arr);
		System.out.println(new String(arr,0,len));

		//3.给客户端反馈
		OutputStream out = socket.getOutputStream();
		out.write("我看到啦".getBytes());
	}
}